package com.xxxx.server.controller;

import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.AdminVO;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.RespPageBean;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.impl.AdminServiceImpl;
import com.xxxx.server.utils.AdminUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: yeb
 * @description: 用户个人中心
 * @author: Honors
 * @create: 2021-08-02 14:58
 */
@RestController
@Api(value = "个人中心", tags = "个人中心")
public class AdminInfoController {

    @Autowired
    private IAdminService iAdminService;

    @ApiOperation(value = "更新当前用户信息")
    @PostMapping("/admin-info")
    // Authentication 这个对象是 Security 提供的可以用他直接获取到当前登录用户的信息，Admin 是前端用户传过来的用户信息
    public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication) {
        if (iAdminService.updateById(admin)) {//只是跟新了数据库
            //更新用户的全局上下文信息
            SecurityContextHolder.getContext().setAuthentication(
                    new UsernamePasswordAuthenticationToken(admin, null, authentication.getAuthorities()));
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败!");
    }

    @ApiOperation(value = "更新用户密码")
    @PostMapping("/admin-pass")
    public RespBean updateAdminPassWord(@RequestBody Admin admin) {
        // 用户头像
        String userFace = admin.getUserFace();
        // 地址
        String address = admin.getAddress();
        // 旧密码
        String password = admin.getTelephone();
        // 新密码
        String pass = admin.getPassword();
        // 电话
        String phone = admin.getPhone();
        return iAdminService.updateAdminPassWord(password, pass, AdminUtils.getCurrentAdmin().getId(), phone, userFace, address);
    }

//    @ApiOperation(value = "跟新用户头像")
//    @PostMapping("/admin/userface")
//    public RespBean updateAdminUserFace(MultipartFile file,
//            Integer id,
//            Authentication authentication) {
//        //上传之后的文件名
//        String[] filePath = FastDFSUtils.upload(file);
//        //获取 url ， 初始文件名，上传之后的文件名
//        String url = FastDFSUtils.getTrackerUrl() + filePath[0] + "/" + filePath[1];
//        return iAdminService.updateAdminUserFace(url, id, authentication);
//    }

    @Resource
    private AdminServiceImpl adminServiceImpl;

    @ApiOperation(value = "新增用户")
    @PostMapping("/addAdmin")
    public RespBean addAdmin(
            @ApiParam("用户对象") @RequestBody Admin admin) {
        return adminServiceImpl.addOrUpdateAdmin(admin);
    }

    @ApiOperation(value = "用户详情")
    @PostMapping("/selectAdminById")
    public AdminVO selectAdminById(
            @ApiParam("用户id") @RequestParam Integer id) {
        return adminServiceImpl.selectAdminById(id);
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/deleteAdmin")
    public RespBean deleteAdmin(
            @ApiParam("用户id") @RequestParam Integer id) {
        return adminServiceImpl.deleteAdmin(id);
    }

    @ApiOperation(value = "批量删除用户")
    @PostMapping("/deleteAdmins")
    public RespBean deleteAdmins(
            @ApiParam("用户id") @RequestBody Integer[] ids) {
        RespBean respBean = null;
        for (Integer id : ids) {
            respBean = adminServiceImpl.deleteAdmin(id);
        }
        return respBean;
    }

    @ApiOperation(value = "获取用户分页")
    @GetMapping("/page")
    public RespPageBean page(
            @ApiParam @RequestParam(defaultValue = "1") Integer currentPage,
            @ApiParam @RequestParam(defaultValue = "10") Integer size,
            @ApiParam @RequestParam(required = false) String text) {
        return adminServiceImpl.getPage(currentPage, size, text);
    }

    @ApiOperation("批量导入用户")
    @PostMapping("/userAdds")
    public RespBean fileUpload(@RequestBody MultipartFile file) throws IOException {
        return adminServiceImpl.batchImport(file);
    }

    @ApiOperation("获取模板文件")
    @PostMapping("/getTemplate")
    public RespBean getTemplate(
            @ApiParam @RequestParam String path) {
        return adminServiceImpl.getTemplate(path);
    }

    @ApiOperation(value = "导出用户")
    @GetMapping(value = "/export/userList", produces = "application/octet-stream")
    public void exportEmp(HttpServletResponse response) {
        adminServiceImpl.userList(response);
    }


}
